package com.jcark.ui.view.textview

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView

import android.graphics.drawable.GradientDrawable
import android.text.TextUtils
import com.jcark.ui.R

class MButton(context: Context, attrs: AttributeSet?) : AppCompatTextView(context, attrs) {
    constructor(context: Context) : this(context, null)

    private var mEnableTextColor: String? = ""
    private var mDisEnableTextColor: String? = ""
    private var mTextSize: Float? = 0f
    private var mText: String? = ""
    private var mRadius: Int? = null
    private var mBgDisEnable: String? = ""
    private var mBgEnable: String? = ""

    private var mEnable: Boolean = true

    init {
        gravity = Gravity.CENTER
        val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.MButton)
        mEnableTextColor = a.getString(R.styleable.MButton_enableTextColor)
        mDisEnableTextColor = a.getString(R.styleable.MButton_disEnableTextColor)
        mTextSize = a.getFloat(R.styleable.MButton_base_textSize, 15f)
        mText = a.getString(R.styleable.MButton_base_text)
        mRadius = a.getDimensionPixelSize(R.styleable.MButton_radius, 0)
        mBgDisEnable = a.getString(R.styleable.MButton_bgDisEnable)
        mBgEnable = a.getString(R.styleable.MButton_bgEnable)
        mEnable = a.getBoolean(R.styleable.MButton_enable, true)
        initData()
        a.recycle()
    }

    private fun initData() {
        textSize = mTextSize as Float
        if (!TextUtils.isEmpty(mText)) {
            text = mText
        }
        val gd = GradientDrawable()
        if (mEnable) {
            if (!TextUtils.isEmpty(mBgEnable)) {
                gd.setColor(Color.parseColor(mBgEnable))
            } else {
                gd.setColor(Color.YELLOW)
            }
            if (!TextUtils.isEmpty(mEnableTextColor)) {
                setTextColor(Color.parseColor(mEnableTextColor))
            } else {
                setTextColor(Color.BLACK)
            }
        } else {
            if (!TextUtils.isEmpty(mBgDisEnable)) {
                gd.setColor(Color.parseColor(mBgDisEnable))
            } else {
                gd.setColor(Color.GRAY)
            }

            if (!TextUtils.isEmpty(mDisEnableTextColor)) {
                setTextColor(Color.parseColor(mDisEnableTextColor))
            } else {
                setTextColor(Color.BLACK)
            }
        }
        gd.cornerRadius = dp2px(mRadius!!.toFloat(), context)
        this.background = gd
    }

    override fun setEnabled(enabled: Boolean) {
        mEnable = enabled
        initData()
    }

    fun setEnableTextColor(enableColor: String) {
        mEnableTextColor = enableColor
        initData()
    }

    fun setDisEnableTextColor(disEnableColor: String) {
        mDisEnableTextColor = disEnableColor
        initData()
    }

    fun setMyText(str: String) {
        mText = str
        initData()
    }

    fun setMyTextSize(size: Float) {
        mTextSize = size
        initData()
    }

    fun setMyRadius(radius: Int) {
        mRadius = radius
        initData()
    }

    fun setDisEnableBgColor(disEnableColor: String) {
        mBgDisEnable = disEnableColor
        initData()
    }

    fun setEnableBgColor(enableColor: String) {
        mBgEnable = enableColor
        initData()
    }

    private fun dp2px(dpValue: Float, context: Context): Float {
        val density = context.resources.displayMetrics.density
        return dpValue * density + 0.5F
    }

}